package com.downhometech.productcalculator.models;

import java.io.Serializable;
import java.util.HashMap;

import android.content.ContentValues;
import android.database.Cursor;

public class Product implements Serializable
{
	private int id = 0;
	private String name = "";
	private String description = "";
	private double listedPrice = 0;
	private boolean category = false;
	private int category_id = 0;
	private String number = "";
	private double customPrice = 0;
	private boolean userConfigured = false;
	private boolean deleted = false;
	
	public double getCustomPrice() {
		return customPrice;
	}

	public void setCustomPrice(double customPrice) {
		this.customPrice = customPrice;
	}

	public boolean isUserConfigured() {
		return userConfigured;
	}

	public boolean isDeleted() {
		return deleted;
	}

	public void setDeleted(boolean deleted) {
		this.deleted = deleted;
	}

	public void setUserConfigured(boolean userConfigured) {
		this.userConfigured = userConfigured;
	}



	public Product() {}
	
	
	
	public Product(Cursor databaseCursor)
	{
        this.setId(databaseCursor.getInt(databaseCursor.getColumnIndex("id")));
        this.setName(databaseCursor.getString(databaseCursor.getColumnIndex("name")));
        this.setDescription(databaseCursor.getString(databaseCursor.getColumnIndex("description")));
        this.setListedPrice(databaseCursor.getDouble(databaseCursor.getColumnIndex("price")));
        this.setCategory(databaseCursor.getInt((databaseCursor.getColumnIndex("category"))) == 1);
        this.setCategoryId(databaseCursor.getInt(databaseCursor.getColumnIndex("categoryId")));
	
        this.setCustomPrice(databaseCursor.getDouble(databaseCursor.getColumnIndex("customPrice")));
        this.setNumber(databaseCursor.getString(databaseCursor.getColumnIndex("number")));
        this.setUserConfigured(databaseCursor.getInt(databaseCursor.getColumnIndex("userConfigured")) == 1);
	}
	
	public Product(int id, String name, String description, double listedPrice, boolean category, int categoryId) {
		this.id = id;
		this.name = name;
		this.description = description;
		this.listedPrice = listedPrice;
		this.category = category;
		this.category_id = categoryId;
	}
	
	public Product(int id, String name, String description)
	{
		this.id = id;
		this.name = name;
		this.description = description;		
	}
	
	public static Product generateBlankProduct(int id) {
		Product product = new Product();
		product.setId(id);
		return product;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public double getPrice() {
		if (isUserConfigured()) { return getCustomPrice(); }
		else return getListedPrice();
	}

	public double getListedPrice() {
		return listedPrice;
	}

	public void setListedPrice(double listedPrice) {
		this.listedPrice = listedPrice;
	}

	public boolean isCategory() {
		return category;
	}
	public void setCategory(boolean category) {
		this.category = category;
	}
	
	public int getCategoryId() {
		return category_id;
	}


	public String getNumber() {
		return number;
	}



	public void setNumber(String number) {
		this.number = number;
	}



	public void setCategoryId(int categoryId) {
		category_id = categoryId;
	}


	public String toString()
	{
		if (getNumber().equals("") || isCategory()) return getName();
		else return getName() + " (#" + getNumber() + ")";
	}

	public ContentValues toContentValue(boolean includeId)
	{	
		ContentValues value = new ContentValues();
		if (includeId) { value.put("id", this.id); }
		value.put("name", this.name);
		value.put("description", this.description);
		value.put("price", this.listedPrice);
		value.put("category", this.category);
		value.put("categoryId", this.category_id);
		value.put("number", this.number);
		value.put("customPrice", this.customPrice);
		value.put("userConfigured", this.userConfigured);
		value.put("deleted", this.deleted);
		return value;
	}
	
	public HashMap<String, Object> toHashMap()
	{
		HashMap<String, Object> hash = new HashMap<String, Object>();
		hash.put("id", new Integer(getId()));
		hash.put("name", getName());
		hash.put("description", getDescription());
		hash.put("price", new Double(getListedPrice()));
		hash.put("category", new Boolean(isCategory()));
		
		hash.put("categoryId", this.category_id);
		hash.put("number", this.number);
		hash.put("customPrice", this.customPrice);
		hash.put("userConfigured", this.userConfigured);
		
		return hash;
	}
	
	
	
}
